package com.easy.controller;

import com.easy.bean.User_activity;
import com.easy.service.User_activityService;
import com.easy.util.Page;
import com.easy.util.Result;
import com.easy.util.SYS;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
@RequestMapping("/user_activity")
public class User_activityController {
    @Autowired
    User_activityService user_activityService;

    @PostMapping
    public Result add(HttpServletRequest request, @RequestBody User_activity user_activity) {
        // 获取当前用户信息
        String token = request.getHeader(SYS.TOKEN_AUTHORIZATION);
        Object roleObj = com.easy.util.TokenUtil.getOption(token, SYS.TOKEN_KEY_ROLE);
        Object idObj = com.easy.util.TokenUtil.getOption(token, SYS.TOKEN_KEY_ID);

        // 设置活动创建者为当前用户
        if (idObj != null) {
            user_activity.setUser_id(Integer.valueOf(idObj.toString()));
        }

        return Result.success(user_activityService.add(user_activity));
    }

    @PutMapping
    public Result edit(HttpServletRequest request, @RequestBody User_activity user_activity) {
        // 获取当前用户信息
        String token = request.getHeader(SYS.TOKEN_AUTHORIZATION);
        Object roleObj = com.easy.util.TokenUtil.getOption(token, SYS.TOKEN_KEY_ROLE);
        Object idObj = com.easy.util.TokenUtil.getOption(token, SYS.TOKEN_KEY_ID);

        // 管理员可以编辑所有活动，普通用户只能编辑自己的活动
        if (roleObj != null && !"admin".equals(roleObj.toString())) {
            if (idObj != null) {
                Integer userId = Integer.valueOf(idObj.toString());
                // 检查活动是否属于当前用户
                User_activity existingActivity = user_activityService.one(user_activity.getId());
                if (existingActivity != null && !existingActivity.getUser_id().equals(userId)) {
                    return Result.fail(403, "只能编辑自己的活动记录");
                }
            }
        }

        return Result.success(user_activityService.edit(user_activity));
    }

    @DeleteMapping("/{id}")
    public Result del(HttpServletRequest request, @PathVariable("id") Integer id) {
        // 获取当前用户信息
        String token = request.getHeader(SYS.TOKEN_AUTHORIZATION);
        Object roleObj = com.easy.util.TokenUtil.getOption(token, SYS.TOKEN_KEY_ROLE);
        Object idObj = com.easy.util.TokenUtil.getOption(token, SYS.TOKEN_KEY_ID);

        // 管理员可以删除所有活动，普通用户只能删除自己的活动
        if (roleObj != null && !"admin".equals(roleObj.toString())) {
            if (idObj != null) {
                Integer userId = Integer.valueOf(idObj.toString());
                // 检查活动是否属于当前用户
                User_activity existingActivity = user_activityService.one(id);
                if (existingActivity != null && !existingActivity.getUser_id().equals(userId)) {
                    return Result.fail(403, "只能删除自己的活动记录");
                }
            }
        }

        return Result.success(user_activityService.del(id));
    }

    @GetMapping("/{id}")
    public Result one(HttpServletRequest request, @PathVariable("id") Integer id) {
        // 获取当前用户信息
        String token = request.getHeader(SYS.TOKEN_AUTHORIZATION);
        Object roleObj = com.easy.util.TokenUtil.getOption(token, SYS.TOKEN_KEY_ROLE);
        Object idObj = com.easy.util.TokenUtil.getOption(token, SYS.TOKEN_KEY_ID);

        // 管理员可以查看所有活动，普通用户只能查看自己的活动
        if (roleObj != null && !"admin".equals(roleObj.toString())) {
            if (idObj != null) {
                Integer userId = Integer.valueOf(idObj.toString());
                // 检查活动是否属于当前用户
                User_activity existingActivity = user_activityService.one(id);
                if (existingActivity != null && !existingActivity.getUser_id().equals(userId)) {
                    return Result.fail(403, "只能查看自己的活动记录");
                }
            }
        }

        return Result.success(user_activityService.one(id));
    }

    @GetMapping("all")
    public Result all(HttpServletRequest request, User_activity user_activity) {
        try {
            // 获取当前用户信息
            String token = request.getHeader(SYS.TOKEN_AUTHORIZATION);
            Object roleObj = com.easy.util.TokenUtil.getOption(token, SYS.TOKEN_KEY_ROLE);
            Object idObj = com.easy.util.TokenUtil.getOption(token, SYS.TOKEN_KEY_ID);

            // 普通用户只能查看自己的活动
            if (roleObj != null && !"admin".equals(roleObj.toString())) {
                if (idObj != null) {
                    user_activity.setUser_id(Integer.valueOf(idObj.toString()));
                }
            }

            return Result.success(user_activityService.all(user_activity));
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(500, "查询失败：" + e.getMessage());
        }
    }

    @GetMapping("page")
    public Result page(HttpServletRequest request, Page page, User_activity user_activity) {
        try {
            // 获取当前用户信息
            String token = request.getHeader(SYS.TOKEN_AUTHORIZATION);
            Object roleObj = com.easy.util.TokenUtil.getOption(token, SYS.TOKEN_KEY_ROLE);
            Object idObj = com.easy.util.TokenUtil.getOption(token, SYS.TOKEN_KEY_ID);

            // 普通用户只能查看自己的活动
            if (roleObj != null && !"admin".equals(roleObj.toString())) {
                if (idObj != null) {
                    user_activity.setUser_id(Integer.valueOf(idObj.toString()));
                }
            }

            user_activityService.page(page, user_activity);
            return Result.success(page);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(500, "分页查询失败：" + e.getMessage());
        }
    }

    @GetMapping("pageWithDetails")
    public Result pageWithDetails(HttpServletRequest request, Page page, User_activity user_activity) {
        try {
            // 获取当前用户信息
            String token = request.getHeader(SYS.TOKEN_AUTHORIZATION);
            Object roleObj = com.easy.util.TokenUtil.getOption(token, SYS.TOKEN_KEY_ROLE);
            Object idObj = com.easy.util.TokenUtil.getOption(token, SYS.TOKEN_KEY_ID);

            // 普通用户只能查看自己的活动
            if (roleObj != null && !"admin".equals(roleObj.toString())) {
                if (idObj != null) {
                    user_activity.setUser_id(Integer.valueOf(idObj.toString()));
                }
            }

            user_activityService.pageWithDetails(page, user_activity);
            return Result.success(page);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(500, "分页查询失败：" + e.getMessage());
        }
    }
}
